package com.iti.jets.gpts.home;

// default package
// Generated Mar 22, 2014 9:41:00 PM by Hibernate Tools 3.4.0.CR1
import com.iti.jets.gpts.dao.AbstractDAO;
import com.iti.jets.gpts.pojo.Feedback;
import com.iti.jets.gpts.pojo.User;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

/**
 * Home object for domain model class User.
 *
 * @see .User
 * @author Hibernate Tools
 */
public class UserHome extends AbstractDAO<User> {

    public UserHome(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    //all users
    public List<User> getAllUser() {
        Criteria criteria = session.createCriteria(User.class).add(Restrictions.eq("isAdmin", Boolean.FALSE));

        List<User> list = criteria.list();
        return list;

    }

    public User getObject(String email) {
        Criteria criteria = session.createCriteria(User.class)
                .add(Restrictions.eq("email", email));
        User user = (User) criteria.list().get(0);
        return user;

    }
    
    public User getUser(String mail,String pass) {
        User user = null;
        session.getTransaction().begin();
        String hqlQuery = "from User u where u.email=? and u.password=?";
        
        Query query = session.createQuery(hqlQuery).setString(0, mail).setString(1, pass);
        user = (User) query.uniqueResult();
        session.getTransaction().commit();
        if(user != null )
             System.out.println("User email is "+user.getEmail());
        else
             System.out.println("User doens't exist");
        return user;
    }
    public boolean isUserPostFeedbackBefore(int id) {
        
        Criteria criteria=session.createCriteria(Feedback.class,"f")
                .add(Restrictions.eq("f.user.id", id));
        List list =criteria.list();
        if(list.size()==0){
       return false;
        }else{
            return true;
        }
    }
    public User getUserEmail(String mail){
         
          User user = null;
        session.getTransaction().begin();
        String hqlQuery = "from User u where u.email=?";
        
        Query query = session.createQuery(hqlQuery).setString(0, mail);
        user = (User) query.uniqueResult();
        session.getTransaction().commit();
        if(user != null )
             System.out.println("User email is "+user.getEmail());
        else
             System.out.println("User doens't exist");
        return user;
     }
}

